<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="laravel-的分页功能" tabindex="-1"><a class="header-anchor" href="#laravel-的分页功能"><span>Laravel 的分页功能</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#basic-usage">基本用法</a>
<ul>
<li><a href="#paginating-query-builder-results">查询构造器分页</a></li>
<li><a href="#paginating-eloquent-results">Eloquent 分页</a></li>
<li><a href="#manually-creating-a-paginator">手动创建分页</a></li>
</ul>
</li>
<li><a href="#displaying-pagination-results">显示分页结果</a>
<ul>
<li><a href="#converting-results-to-json">将结果转换为 JSON</a></li>
</ul>
</li>
<li><a href="#customizing-the-pagination-view">自定义分页视图</a></li>
<li><a href="#paginator-instance-methods">分页器实例方法</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>在大多数的框架中，分页无不令人十分头疼。 Laravel 的分页器与<a href="https://learnku.com/docs/laravel/5.5/queries" target="_blank" rel="noopener noreferrer">查询构造器</a>、<a href="https://learnku.com/docs/laravel/5.5/eloquent" target="_blank" rel="noopener noreferrer">Eloquent ORM</a> 集成在一起，并提供方便易用的数据结果集分页。分页器生成的 HTML 与 Bootstrap CSS 框架兼容。</p>
<h2 id="基本用法" tabindex="-1"><a class="header-anchor" href="#基本用法"><span>基本用法</span></a></h2>
<h3 id="查询构造器分页" tabindex="-1"><a class="header-anchor" href="#查询构造器分页"><span>查询构造器分页</span></a></h3>
<p>有几种方法可以对数据进行分页。最简单的是在 <a href="https://learnku.com/docs/laravel/5.5/queries" target="_blank" rel="noopener noreferrer">查询语句构造器</a> 或 <a href="https://learnku.com/docs/laravel/5.5/eloquent" target="_blank" rel="noopener noreferrer">Eloquent 查询</a> 中使用 <code v-pre>paginate</code> 方法。 <code v-pre>paginate</code> 方法会自动根据用户正在查看的页面来设置限制和偏移量。默认情况下，当前页面通过 HTTP 请求所带的参数 <code v-pre>page</code> 的值来检测。这个值是被 Laravel 自动检测到的，也会自动插入到由分页器生产的链接中。</p>
<p>在下面这个例子中，传递给 <code v-pre>paginate</code> 方法的唯一参数是「每页」显示的项目数量。下面是每页显示 <code v-pre>15</code> 条数据的例子：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>Controller</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 展示应用中的所有用户</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name">Response</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">index</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.index'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'users'</span> <span class="token operator">=></span> <span class="token variable">$users</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>{note} 目前，Laravel 无法高效执行使用 <code v-pre>groupBy</code> 语句的分页操作。如果你需要在分页结果集中使用 <code v-pre>groupBy</code>，建议你查询数据库并手动创建分页器。</p>
</blockquote>
<h4 id="「简单分页」" tabindex="-1"><a class="header-anchor" href="#「简单分页」"><span>「简单分页」</span></a></h4>
<p>如果你只需要在分页视图中显示简单的「下一页」和「上一页」的链接，即不需要显示每个页码的链接，更推荐使用 <code v-pre>simplePaginate</code> 方法来执行更高效的查询：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="eloquent-模型分页" tabindex="-1"><a class="header-anchor" href="#eloquent-模型分页"><span>Eloquent 模型分页</span></a></h3>
<p>你也可以对 <a href="https://learnku.com/docs/laravel/5.5/eloquent" target="_blank" rel="noopener noreferrer">Eloquent</a> 查询进行分页。下面的例子中对 <code v-pre>User</code> 模型进行了分页并且每页显示 <code v-pre>15</code> 条数据。正如你看到的，所使用的语法几乎与基于查询语句构造器分页时的完全相同：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>当然，你可以在设置了查询的其他约束条件之后调用 <code v-pre>paginate</code> 方法，例如 <code v-pre>where</code> 语句：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你也可以在 <code v-pre>Eloquent</code> 模型中使用 <code v-pre>simplePaginate</code> 方法进行分页：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'>'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">simplePaginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="手动创建分页" tabindex="-1"><a class="header-anchor" href="#手动创建分页"><span>手动创建分页</span></a></h3>
<p>如果你想手动创建分页实例并且最终得到一个数组类型的结果，可以根据需求来创建 <code v-pre>Illuminate\Pagination\Paginator</code> 或者 <code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 实例来实现。</p>
<p><code v-pre>Paginator</code> 类不需要知道结果集中的数据量，因此该类没有检索最后一页索引的方法。而 <code v-pre>LengthAwarePaginator</code> 接收的参数几乎和 <code v-pre>Paginator</code> 一样，但它却需要计算结果集中的数据量。</p>
<p>说白了就是，<code v-pre>Paginator</code> 相当于查询语句构造器和 Eloquent 中的 <code v-pre>simplePaginate</code> 方法，而 <code v-pre>LengthAwarePaginator</code> 相当于 <code v-pre>paginate</code> 方法。</p>
<blockquote>
<p>{note} 手动创建分页器实例时，你应该手动「切割」传递给分页器的结果集数组。如果你不确定如何去做到这一点，请查阅 PHP 的 <a href="https://secure.php.net/manual/en/function.array-slice.php" target="_blank" rel="noopener noreferrer">array_slice</a> 函数。</p>
</blockquote>
<h2 id="显示分页结果" tabindex="-1"><a class="header-anchor" href="#显示分页结果"><span>显示分页结果</span></a></h2>
<p>在调用 <code v-pre>paginate</code> 方法时，你将会接收到一个 <code v-pre>Illuminate\Pagination\LengthAwarePaginator</code> 实例。当调用 <code v-pre>simplePaginate</code> 方法时，你将会接收到一个 <code v-pre>Illuminate\Pagination\Paginator</code> 实例。这些对象提供了一些用于渲染结果集的函数。除了这些辅助函数，分页器实例是一个迭代器，也可以作为数组循环。因此，一旦检测到结果集，你可以使用 <a href="https://learnku.com/docs/laravel/5.5/blade" target="_blank" rel="noopener noreferrer">Blade</a> 模板显示结果集并渲染页面链接：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>div <span class="token keyword">class</span><span class="token operator">=</span><span class="token string double-quoted-string">"container"</span><span class="token operator">></span></span>
<span class="line">    @<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line">    @<span class="token keyword">endforeach</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>links</code> 方法将会链接渲染到结果集中其余的页面。每个链接都包含了正确的 <code v-pre>page</code> 查询字符串变量。记住，<code v-pre>links</code> 方法生成的 HTML 与 <a href="https://getbootstrap.com/" target="_blank" rel="noopener noreferrer">Bootstrap CSS 框架</a> 兼容。</p>
<h4 id="自定义分页器的-uri" tabindex="-1"><a class="header-anchor" href="#自定义分页器的-uri"><span>自定义分页器的 URI</span></a></h4>
<p><code v-pre>withPath</code> 方法允许你在生成链接时自定义分页器所使用的 URI。例如，如果你想要分页器生成像 <code v-pre>http://example.com/custom/url?page=N</code> 这样的链接，那就传递 <code v-pre>custom/url</code> 到 <code v-pre>withPath</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">withPath</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'custom/url'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">//</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="附加参数到分页链接中" tabindex="-1"><a class="header-anchor" href="#附加参数到分页链接中"><span>附加参数到分页链接中</span></a></h4>
<p>你可以使用 <code v-pre>appends</code> 方法将查询参数附加到分页链接中。例如，要将 <code v-pre>sort=votes</code> 附加到每个分页链接，可以这样调用 <code v-pre>appends</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">appends</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sort'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'votes'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果想将「哈希片段」附加到分页器的链接中，就使用 <code v-pre>fragment</code> 方法。例如，要将 <code v-pre>#foo</code> 附加到每个分页链接的末尾，应该这样调用 <code v-pre>fragment</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">fragment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="将结果转换为-json" tabindex="-1"><a class="header-anchor" href="#将结果转换为-json"><span>将结果转换为 JSON</span></a></h3>
<p>Laravel 分页器结果类实现了 <code v-pre>Illuminate\Contracts\Support\Jsonable</code> 接口契约并且提供 <code v-pre>toJson</code> 方法，因此将分页结果转换为 JSON 非常简单。你也可以在路由或控制器操作中简单地将分页实例转换为 JSON 返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>分页器中获取的 JSON 将包含元信息，如：<code v-pre>total</code>、<code v-pre>current_page</code>、<code v-pre>last_page</code> 等等。实际的结果对象将通过 JSON 数组中的 <code v-pre>data</code> 键来获取。 以下是一个从路由返回的分页器实例创建的 JSON 示例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">   <span class="token string double-quoted-string">"total"</span><span class="token punctuation">:</span> <span class="token number">50</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"per_page"</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"current_page"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"last_page"</span><span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"first_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=1"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"last_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=4"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"next_page_url"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app?page=2"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"prev_page_url"</span><span class="token punctuation">:</span> <span class="token constant">null</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"path"</span><span class="token punctuation">:</span> <span class="token string double-quoted-string">"http://laravel.app"</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"from"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"to"</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span></span>
<span class="line">   <span class="token string double-quoted-string">"data"</span><span class="token punctuation">:</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// Result Object</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">// Result Object</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">   <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义分页视图" tabindex="-1"><a class="header-anchor" href="#自定义分页视图"><span>自定义分页视图</span></a></h2>
<p>默认情况下，渲染分页链接的视图与 Bootstrap CSS 框架兼容。但是，如果你不使用 Bootstrap，可以自由定义视图来渲染这些链接。在分页器实例中调用 <code v-pre>links</code> 方法时，只需将视图名称作为第一个参数传递给方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$paginator</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Passing data to the view...</span></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$paginator</span><span class="token operator">-></span><span class="token function">links</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'view.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>自定义分页视图的制作最简单的方法是使用 <code v-pre>vendor:publish</code> 命令将它们导出到 <code v-pre>resources/views/vendor</code> 目录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">php artisan vendor<span class="token punctuation">:</span>publish <span class="token operator">--</span>tag<span class="token operator">=</span>laravel<span class="token operator">-</span>pagination</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>这个命令将这些视图放置在 <code v-pre>resources/views/vendor/pagination</code> 目录中。这个目录下的 <code v-pre>default.blade.php</code> 文件对应默认的分页视图，你可以简单地编辑这个文件来修改分页的 HTML 。</p>
<h2 id="分页器实例方法" tabindex="-1"><a class="header-anchor" href="#分页器实例方法"><span>分页器实例方法</span></a></h2>
<p>每个分页器实例可以通过以下方法获取额外的分页信息：</p>
<ul>
<li><code v-pre>$results-&gt;count()</code></li>
<li><code v-pre>$results-&gt;currentPage()</code></li>
<li><code v-pre>$results-&gt;firstItem()</code></li>
<li><code v-pre>$results-&gt;hasMorePages()</code></li>
<li><code v-pre>$results-&gt;lastItem()</code></li>
<li><code v-pre>$results-&gt;lastPage()</code> （使用 simplePaginate 时不可用）</li>
<li><code v-pre>$results-&gt;nextPageUrl()</code></li>
<li><code v-pre>$results-&gt;perPage()</code></li>
<li><code v-pre>$results-&gt;previousPageUrl()</code></li>
<li><code v-pre>$results-&gt;total()</code> （使用 simplePaginate 时不可用）</li>
<li><code v-pre>$results-&gt;url($page)</code></li>
</ul>
<h2 id="译者署名" tabindex="-1"><a class="header-anchor" href="#译者署名"><span>译者署名</span></a></h2>
<table>
<thead>
<tr>
<th>用户名</th>
<th>头像</th>
<th>职能</th>
<th>签名</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="https://github.com/ChrisonWang" target="_blank" rel="noopener noreferrer">@ChrisonWang</a></td>
<td><img src="https://avatars0.githubusercontent.com/u/16531947?v=4&amp;s=80" alt="16531947?v=4&amp;s=80"></td>
<td></td>
<td></td>
</tr>
<tr>
<td>翻译</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="https://www.linkedin.com/in/ChrisonWang/" target="_blank" rel="noopener noreferrer">@王欣</a> at LinkedIn</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td><a href="https://learnku.com/users/5350" target="_blank" rel="noopener noreferrer">@JokerLinly</a></td>
<td><img src="https://cdn.learnku.com/uploads/avatars/5350_1481857380.jpg" alt="5350_1481857380.jpg"></td>
<td>Review</td>
<td>Stay Hungry. Stay Foolish.</td>
</tr>
</tbody>
</table>
<hr>
<blockquote>
<p>{note} 欢迎任何形式的转载，但请务必注明出处，尊重他人劳动共创开源社区。</p>
<p>转载请注明：本文档由 Laravel China 社区 <a href="https://laravel-china.org/" target="_blank" rel="noopener noreferrer">laravel-china.org</a> 组织翻译，详见 <a href="https://learnku.com/laravel/t/5756/laravel-55-document-translation-call-come-and-join-the-translation" target="_blank" rel="noopener noreferrer">翻译召集帖</a>。</p>
<p>文档永久地址： <a href="https://learnku.com/docs/laravel" target="_blank" rel="noopener noreferrer">《Laravel 中文文档》</a></p>
</blockquote>
</div></template>


